package com.haircut.project.controller.internal;

import cn.hutool.core.date.DateUtil;
import com.haircut.project.service.finance.IDeptCalcService;
import com.haircut.project.service.finance.IStaticFinanceService;
import com.haircut.scheduled.RecordScheduledService;
import com.modular.redis.RedisUtils;
import com.modular.result.RspMessage;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("internal/finance")
public class InternalFinanceController {
    private final Logger logger = LoggerFactory.getLogger(InternalFinanceController.class);
    @Autowired
    private IStaticFinanceService staticFinanceService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    @Qualifier("salaryExecutor")
    private AsyncTaskExecutor asyncTaskExecutor;
    @Autowired
    private IDeptCalcService deptCalcService;

    /**
     * 查询分类列表
     * @return
     */
    @ApiOperation(value = "内部执行")
    @GetMapping("staticMonthDay")
    public RspMessage<Void> staticMonthDay(){
        logger.info("staticMonthDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:batch:month";
        if(redisUtils.checkRedisKey(reidsKey)){
            return RspMessage.success();
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                staticFinanceService.staticWeekDay(reidsKey);
            }catch (Exception e){
                logger.error("staticMonthDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });
        return RspMessage.success();
    }
    /**
     * 查询分类列表
     * @return
     */
    @ApiOperation(value = "内部执行")
    @GetMapping("staticCurrDay")
    public RspMessage<Void> staticCurrDay(){
        logger.info("staticCurrDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:batch:curr";
        if(redisUtils.checkRedisKey(reidsKey)){
            return RspMessage.success();
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                staticFinanceService.staticCurrDay(reidsKey);
            }catch (Exception e){
                logger.error("staticCurrDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });
        return RspMessage.success();
    }

    /**
     * 每天 1，2，3五个小时 5分执行
     */
    @ApiOperation(value = "内部执行")
    @GetMapping("calcDeptFinance")
    public RspMessage<Void> calcDeptFinance() {
        logger.info("staticCurrDay date {}", DateUtil.formatDateTime(DateUtil.date()));
        String reidsKey  = "finance:detp:calc";
        if(redisUtils.checkRedisKey(reidsKey)){
            return RspMessage.success();
        }
        // 刷新缓存
        redisUtils.flushRedis(reidsKey);
        asyncTaskExecutor.execute(()->{
            try {
                deptCalcService.calcDeptFinance(reidsKey);
            }catch (Exception e){
                logger.error("staticCurrDay err ",e);
            }finally {
                redisUtils.delKey(reidsKey);
            }
        });
        return RspMessage.success();
    }
}
